/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.solaris;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 *
 * @author Frank
 */
public class ActivityAddObservation extends Activity {

    private Connection connection;
    private Session session;
    private HashMap telescopen;
    
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        
        super.onCreate(icicle);     
        setContentView(R.layout.addobservation);    
        
        ActionBar actionBar = getActionBar();
        actionBar.show();
        
        // Connectieobject ophalen
        this.connection = Connection.instantiate();
        this.session = Session.instantiate();
        
        // Alle telescopen van de gebruiker ophalen
        this.telescopen = this.connection.getTelescopen(session.getUsername(), session.getPassword());
        
        if(telescopen.size() > 0){
            
            // Tijdelijke lijst aanleggen voor de spinner
            ArrayList<String> spinnerTelescoop = new ArrayList<String>();
        
            Collection c = this.telescopen.values();
            Iterator itr = c.iterator();

            // Doorloop de ontvangen waarden
            while(itr.hasNext()){
                spinnerTelescoop.add(itr.next().toString());
            }

            // Spinner vullen met te selecteren items
            Spinner telescoop = (Spinner) findViewById(R.id.telescoop);
            ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerTelescoop);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            telescoop.setAdapter(adapter);
        
        } else {
    
            // Melding weergeven aan gebruiker
            AlertDialog alertDialog = new AlertDialog.Builder(ActivityAddObservation.this).create();               
            alertDialog.setTitle("Geen telescoop");
            alertDialog.setMessage("U kunt geen observatie toevoegen. U dient via de website eerst een telescoop toe te voegen aan uw account.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    
                    // intent weer afsluiten
                    finish();
                    
                }
            });
            alertDialog.show();
            
        }
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // We vullen de actionbar 
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.addobservation, menu);
        return true;
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        
        switch(item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            
            case R.id.observation_save:
                
                // Locationobject ophalen om locatie te kunnen bepalen
                LocationAdapter locationAdapter = LocationAdapter.instantiate(this);
                String locatie = locationAdapter.getLocationAsString();

                // Textvelden ophalen
                final EditText txtSeeing = (EditText) findViewById(R.id.txtSeeing);
                final EditText txtTransparency = (EditText) findViewById(R.id.txtTransparency);
                final EditText txtGroupNorth = (EditText) findViewById(R.id.txtGroupNorth);
                final EditText txtSpotNorth = (EditText) findViewById(R.id.txtSpotNorth);
                final EditText txtGroupSouth = (EditText) findViewById(R.id.txtGroupSouth);
                final EditText txtSpotSouth = (EditText) findViewById(R.id.txtSpotSouth);
                final Spinner txtTelescoop = (Spinner) findViewById(R.id.telescoop);
                final EditText txtCorrection = (EditText) findViewById(R.id.txtCorrection);
                
                // Text uit de componenten halen
                String seeing = txtSeeing.getText().toString();
                String transparency = txtTransparency.getText().toString();
                String groupNorth = txtGroupNorth.getText().toString();
                String spotNorth = txtSpotNorth.getText().toString();
                String groupSouth = txtGroupSouth.getText().toString();
                String spotSouth = txtSpotSouth.getText().toString();
                String telescoop = txtTelescoop.getSelectedItem().toString();
                String correction = txtCorrection.getText().toString();
                
                if(!seeing.equals("") && 
                   !transparency.equals("") &&
                   !groupNorth.equals("") &&
                   !spotNorth.equals("") &&
                   !groupSouth.equals("") &&
                   !spotSouth.equals("") &&
                   !telescoop.equals("") &&
                   !telescoop.equals("")){
                    
                    // Poging tot opslaan
                    Connection connection = Connection.instantiate();
                    boolean opslaanGelukt = connection.saveObservation(seeing, transparency, groupNorth, spotNorth, groupSouth, spotSouth, locatie, telescoop, correction);

                    if(opslaanGelukt){

                        Context context = getApplicationContext();
                        CharSequence tekst = "Opslaan succesvol";
                        int duration = Toast.LENGTH_LONG;

                        Toast toast = Toast.makeText(context, tekst, duration);
                        toast.show();

                        // Opslaan gelukt, terug naar hoofdscherm
                        finish();

                    } else {

                        // Melding weergeven aan gebruiker
                        AlertDialog alertDialog = new AlertDialog.Builder(ActivityAddObservation.this).create();               
                        alertDialog.setTitle("Opslaan mislukt");
                        alertDialog.setMessage("Het opslaan is mislukt, probeer opnieuw!");
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which){
                                // Niks doen op OK klik
                            }
                        });
                        alertDialog.show();

                    }
                    
                } else {
                    
                    // Melding weergeven aan gebruiker
                    AlertDialog alertDialog = new AlertDialog.Builder(ActivityAddObservation.this).create();               
                    alertDialog.setTitle("Niet opgeslagen");
                    alertDialog.setMessage("Niet alle waarden zijn correct ingevuld. Let op! Observatie niet opgeslagen.");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which){
                            // Niks doen op OK klik
                        }
                    });
                    alertDialog.show();
                    
                }
                
                break;
        }
        
        return true;
        
    }
    
}
